﻿using System;
using System.Configuration;
using System.Data;
using System.Text;

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace dcwj
{
    public partial class js : System.Web.UI.Page
    {
        private String M_CONSTR = ConfigurationManager.ConnectionStrings["dcwjConnectionString"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
                if (Request["paperID"] != null)
                {
                    getPaperInfo(Request["paperID"].ToString());
                }
                btnSave.Attributes.Add("onclick", "return checkSel();");
            }
        }

        private void getPaperInfo(string pID)
        {
            if (string.IsNullOrEmpty(pID)) return;
            Database objDB = new SqlDatabase(M_CONSTR);
            if (objDB != null)
            {
                DataSet ds = objDB.ExecuteDataSet(CommandType.Text, @"SELECT PaperName,
                                                                        (select BrandName from Table_BRAND where BrandID = p.BrandID) as BrandName,
                                                                        (select RegionName from Table_REGION where RegionID = p.RegionID) as RegionName,
                                                                        (select PositionName from Table_POSITION where PositionID = p.PositionID) as PositionName,
                                                                        (select TeacherName from Table_TEACHER where TeacherID = p.TeacherID) as TeacherName,
                                                                        CONVERT(varchar(100), StartTime, 23) as StartTime,
                                                                        CONVERT(varchar(100), EndTime, 23) as EndTime 
                                                                        FROM Table_PAPER p where PaperID=" + pID);
                if (ds != null && ds.Tables[0].Rows.Count.Equals(1))
                {
                    lblPaperNm.Text = ds.Tables[0].Rows[0][0].ToString();
                    lblBrand.Text = ds.Tables[0].Rows[0][1].ToString();
                    lblRegion.Text = ds.Tables[0].Rows[0][2].ToString();
                    lblPosition.Text = ds.Tables[0].Rows[0][3].ToString();
                    lblTeacher.Text = ds.Tables[0].Rows[0][4].ToString();
                    lblDtStart.Text = ds.Tables[0].Rows[0][5].ToString();
                    lblDtEnd.Text = ds.Tables[0].Rows[0][6].ToString();
                }
            }
        }

        private void bind()
        {
            Database objDB = new SqlDatabase(M_CONSTR);
            if (objDB != null)
            {
                DataSet ds = objDB.ExecuteDataSet(CommandType.Text, "SELECT * FROM Table_MANYIDU ORDER BY CODEVALUE DESC");
                foreach (Control ctl in this.Page.Form.Controls)
                {
                    string name = ctl.ID;
                    if (!string.IsNullOrEmpty(name) && name.StartsWith("rdoList"))
                    {
                        ((RadioButtonList)ctl).DataSource = ds.Tables[0];
                        ((RadioButtonList)ctl).DataTextField = "CodeName";
                        ((RadioButtonList)ctl).DataValueField = "CodeValue";
                        ((RadioButtonList)ctl).DataBind();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Database objDB = new SqlDatabase(M_CONSTR);
            if (objDB != null)
            {
                 string paperID = string.Empty;
                        string qID = string.Empty;
                        string qAnswer = string.Empty;
                int n = 0;
                foreach (Control ctl in this.Page.Form.Controls)
                {
                    string name = ctl.ID;
                    if (!string.IsNullOrEmpty(name) && (name.StartsWith("rdoList") || name.StartsWith("txt")))
                    {                      
                        if (Request["paperID"] != null)
                        {
                            paperID = Request["paperID"].ToString();
                        }
                        else
                        {
                            paperID = "-1";
                        }
                      
                        if (name.StartsWith("rdoList"))
                        {
                            qAnswer = ((RadioButtonList)ctl).SelectedValue;
                            qID = (name.Length == 8) ? name.Substring(name.Length - 1, 1) : name.Substring(name.Length - 2, 2);
                        }
                        else if (name.StartsWith("txt"))
                        {
                            qAnswer = ((HtmlTextArea)ctl).InnerText;
                            if (name.Equals("txt1"))
                            {
                                qID = "11";
                            }
                            else if (name.Equals("txt2"))
                            {
                                qID = "12";
                            }
                        }

                        StringBuilder sb = new StringBuilder();
                        
                        sb.Append("INSERT INTO Table_RESULT (PaperID,QID,QAnswer) VALUES  (");
                        sb.Append(paperID);
                        sb.Append(",");
                        sb.Append(qID);
                        sb.Append(",");
                        sb.Append("'");
                        sb.Append(qAnswer);
                        sb.Append("'");
                        sb.Append(")");

                        n += objDB.ExecuteNonQuery(CommandType.Text, sb.ToString());
                    }
                }

                if (n >= 10)
                {
                    //更新次数
                    objDB.ExecuteNonQuery(CommandType.Text, "UPDATE Table_PAPER SET SubmitTimes=SubmitTimes+1 WHERE PaperID=" + paperID);
                    Response.Write("<Script Language='JavaScript'>window.alert('" + "提交成功，感谢您的参与配合!" + "');</script>");
                    Response.Write("<script language=javascript>window.navigate(location);</script>");
                }
                else
                {
                    Response.Write("<Script Language='JavaScript'>window.alert('" + "提交失败!" + "');</script>");
                    Response.Write("<script language=javascript>window.close();</script>");
                }
            }
        }
    }
}